package com.example.networkrequestencapsulation;

import android.app.Activity;
import android.content.Context;

import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Stack;

/**
 * 作者 : chenyixi
 * 日期 : 2019/11/19 18:34
 * 文件包名 : com.example.networkrequestencapsulation
 * 描述 :
 */
public class AppManager {

    private static Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        if (null != activityStack) {
            try {
                Activity activity = activityStack.lastElement();
                return activity;
            } catch (NoSuchElementException e) {

            }
        }
        return null;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        if (null != activityStack) {
            Activity activity = activityStack.lastElement();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public void removeActivity(Activity activity) {
        if (activity != null && null != activityStack) {
            activityStack.remove(activity);
        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && null != activityStack) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if (null != activityStack) {
            List<Activity> activities = new LinkedList<Activity>();
            //查找需要结束的Activity
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls)) {
                    activities.add(activity);
                }
            }
            //结束Activity
            for (Activity activity : activities) {
                finishActivity(activity);
            }

        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (null != activityStack) {
            for (Activity activity : activityStack) {
                activity.finish();
            }
            activityStack.clear();
        }
    }

    /**
     * 结束所有订单Activity
     */
    public void finishAllOrderActivity() {
        if (null != activityStack) {
            List<Activity> activities = new LinkedList<Activity>();
            activities.addAll(activityStack);
            for (Activity activity : activities) {
                activity.finish();
                activityStack.remove(activity);
            }
        }
    }



    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
